package com.tfswx.jcyw.sqlmodel.api.bean.table.expression;

import com.tfswx.jcyw.sqlmodel.api.bean.table.Expression;
import com.tfswx.jcyw.sqlmodel.api.bean.table.ExpressionList;
import lombok.Data;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @author 张述江
 * @date 2023/9/15
 */
@Data
public class Function implements Expression {

    private String functionName;

    private ExpressionList parameters;

    public Function(String functionName, Expression... expressions) {
        this.functionName = functionName;
        if (expressions != null) {
            parameters = new ExpressionList();
            parameters.addAll(Arrays.asList(expressions));
        }
    }

    public Function() {
        this(null);
    }

    @Override
    public String toString() {
        String params = "()";
        if (parameters != null && !parameters.isEmpty()) {
            String collect = parameters.stream().map(String::valueOf).collect(Collectors.joining(","));
            params = "(" + collect + ")";
        }

        return functionName + params;
    }
}
